﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Channels;
using System.Text;
using System.Threading.Tasks;
using Hopscotch.Common.Contracts.AppService;
using Hopscotch.Common.Services.Wcf;
using Hopscotch.Domain.Finance;

namespace Hopscotch.Common.Communication.WcfClients
{
    public class CurrencyInstrumentServiceClient : DomainServiceClient<CurrencyInstrument, int>, ICurrencyInstrumentService
    {
        private readonly IClientProxyFactory<ICurrencyInstrumentService> currencyInstrumentServiceFactory;

        public CurrencyInstrumentServiceClient(IClientProxyFactory<ICurrencyInstrumentService> currencyInstrumentServiceFactory)
            : base(currencyInstrumentServiceFactory)
        {
            this.currencyInstrumentServiceFactory = currencyInstrumentServiceFactory;
        }

        public ICollection<CurrencyInstrument> GetAll()
        {
            var client = currencyInstrumentServiceFactory.CreateChannel();
            try
            {
                var currencies = client.GetAll();
                ((IChannel)client).Close();

                return currencies;
            }
            catch (Exception)
            {
                ((IChannel)client).Abort();
                throw;
            }
        }

        public CurrencyInstrument Get(string code)
        {
            var client = currencyInstrumentServiceFactory.CreateChannel();
            try
            {
                var entity = client.Get(code);
                ((IChannel)client).Close();

                return entity;
            }
            catch (Exception)
            {
                ((IChannel)client).Abort();
                throw;
            }
        }
    }
}
